#!/bin/bash -eu
#
# Copyright 2017 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Magento cache, var, media permission setup

cd /var/www/html
mkdir -p var
mkdir -p media
mkdir -p includes

# We set directories permissions to 700, and files to 600 to ensure the running
# PHP code can upload content, write to cache, or rewrite it's own config file

find var/ -type f -exec chmod 600 {} \;
find media/ -type f -exec chmod 600 {} \;
find var/ -type d -exec chmod 700 {} \;
find media/ -type d -exec chmod 700 {} \;
chmod 700 includes

# Magento cron and cache setup

php /var/www/html/bin/magento indexer:reindex
php /var/www/html/bin/magento cache:clean
php /var/www/html/bin/magento cache:flush

# The below command is needed because its important to allow magento to edit its
# own config files or write cache. When we execute bin/magento files are created
# as root as that is what the configuration script is running as. The below
# command ensures all files in var are owned by the user and group www-data.

chown -R www-data:www-data var/
